package cn.codeforfun.jfinalplugin.activemq.core;

import com.jfinal.log.Log;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Map;

//public abstract class JFinalQueue implements Runnable, MessageListener {
public abstract class JFinalQueue extends JFinalActiveMq {

    private static Log log = Log.getLog(JFinalQueue.class);

//    private String user = ActiveMQConnection.DEFAULT_USER;
//    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
//    private String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
    private String queueName = null;
//    private ConnectionFactory connectionFactory;
//    private Connection connection = null;
//    private Session session;
//    private Destination destination;
//    private MessageProducer producer;
//    private MessageConsumer consumer;
//    volatile boolean isStarted = false;

//    public JFinalQueue(String queueName) {
//        super(queueName);
//        this.queueName = queueName;
//        init();
//    }

    static Map<String, JFinalQueue> queueMap = new HashMap<>();

//    public JFinalQueue setUser(String user) {
//        this.user = user;
//        return this;
//    }
//
//    public JFinalQueue setPassword(String password) {
//        this.password = password;
//        return this;
//    }
//
//    public JFinalQueue setBrokerUrl(String url) {
//        this.brokerUrl = url;
//        return this;
//    }
//
//    public JFinalQueue setQueueName(String queueName) {
//        this.queueName = queueName;
//        return this;
//    }

    protected String getQueueName() {
        return queueName;
    }
//    public MessageConsumer getConsumer(){ return consumer().consumer; }

//    public static boolean check_message(String message){
//        if (StrKit.isBlank(message)) {
//            log.debug("Message is blank.");
//            return false;
//        }
//        return true;
//    }

    static boolean check_queueName(String queueName){
        if (!queueMap.containsKey(queueName)) {
            log.error("The queue is not exist.The queue name is " + queueName);
            return false;
        }
        return true;
    }

//    public JFinalQueue connection(){
//        connectionFactory = new ActiveMQConnectionFactory(
//                ActiveMQConnection.DEFAULT_USER,
//                ActiveMQConnection.DEFAULT_PASSWORD,
//                ActiveMQConnection.DEFAULT_BROKER_URL);
//        try { // 构造从工厂得到连接对象
//            connection = connectionFactory.createConnection();
//            connection.start(); // 启动
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

//    public JFinalQueue destination(String queueName){
//        try {
//            // 获取操作连接
//            session = connection.createSession(Boolean.FALSE,
//                    Session.AUTO_ACKNOWLEDGE);
//            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置
//            destination = session.createQueue(queueName);
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }
//
//    public JFinalQueue destination(){
//        return destination(queueName);
//    }
//
//    public JFinalQueue consumer(Destination destination){
//        try {
//            consumer = session.createConsumer(destination);
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }
//
//    public JFinalQueue consumer(){
//        return consumer(destination);
//    }
//
//    public JFinalQueue init(){
//        if (queueName == null) {
//            log.error("Queue name can't be null.");
//            throw new NullPointerException();
//        } else {
//            connection();
//            destination();
//        }
//        return this;
//    }

    //cmd=(start,stop)
    static void optQueue(String cmd){
        JFinalQueue.queueMap.forEach((k,v)->{
            switch (cmd) {
                case "start":
                    try {
                        if(v.isStarted() || v.startMq()){
                            log.debug("The queue has been started.The name is " + v.getQueueName());
                        }
                        log.info("JFinal queue has been started.");
                    } catch (Exception e) {
                        log.info("JFinal queue start error.");
                        e.printStackTrace();
                    }
                    break;

                case "stop":
                    try {
                        if(v.isStarted() && v.stopMq()){
                            log.debug("The queue has been stopped.The name is " + v.getQueueName());
                        }
                        log.info("JFinal queue has been stopped.");
                    } catch (Exception e) {
                        log.info("JFinal queue stop error.");
                        e.printStackTrace();
                    }
                    break;
            }
        });
    }

//    public boolean startQueue() {
//        if (isStarted) return true;
//        Thread thread = new Thread(this);
//        thread.start();
//        isStarted = true;
//        return true;
//    }
//
//    public boolean stopQueue() {
//        if (!isStarted) return true;
//        try {
//            this.connection.close();
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        isStarted = false;
//        return true;
//    }

    public static void sendMessage(String queueName, String message) {
        if(check_message(message) && check_queueName(queueName)){
            JFinalQueue queue = queueMap.get(queueName);
            try {
                TextMessage textMessage = queue.getSession().createTextMessage();
                textMessage.setText(message);
                MessageProducer producer = queue.getSession().createProducer(queue.getDestination());
                producer.send(textMessage);
            } catch (JMSException e) {
                log.error("Send message error.");
                e.printStackTrace();
            }
        }
    }

//    public abstract void onMessage(Message message);
}
